package inimigos;

import org.anddev.andengine.audio.sound.SoundManager;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.TextureManager;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.content.Context;

public class BolaDeCanhao extends Inimigo {

	private BitmapTextureAtlas mapaDeTexturas;
	private TiledTextureRegion textura;
	public int velocidadeY = 1;
	public boolean jaQuicou = false;
	int timerEspera = 0;//MAX = 150
	boolean horaDeEsperar = false;

	public BolaDeCanhao(int x, int y, Context contexto, TextureManager tm, SoundManager sm){
		altura = 64;
		largura = 64;
		this.x = x - altura;
		this.y = y;
		
		mapaDeTexturas = new BitmapTextureAtlas(64, 64, TextureOptions.BILINEAR);
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/inimigos/");
		textura = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mapaDeTexturas, contexto, "Bola de canhao.png", 0, 0, 1, 1);
		tm.loadTexture(mapaDeTexturas);

		spriteAnimado = new AnimatedSprite(x, y, textura);
	}
	
	@Override
	public void atualiza() {
		if(!horaDeEsperar){
			if(y < 480){
				velocidadeY += 2.5f;
				y+=velocidadeY;
				if(y + altura > 416 && !jaQuicou){
					jaQuicou = true;
					velocidadeY = -32;
				}
			}
			if(y > 480){
				if(jaQuicou){
					horaDeEsperar = true;
				}
				jaQuicou = false;
				y = -64;
			}
		}else{
			timerEspera++;
			if(timerEspera == 150) horaDeEsperar = false;
		}
	}

	@Override
	public void posicionaNaCena(int mapx, int mapy) {
		spriteAnimado.setPosition((int)x-mapx, (int)y-mapy);
	}

	@Override
	public boolean colidiuComCaoGuia(AnimatedSprite spriteCaoGuia) {
		if(spriteCaoGuia.collidesWith(spriteAnimado)){
			return true;
		}else{
			return false;
		}
	}

}
